%{
#include <stdio.h>
%}

%union {
	int intval;
	double floatval;
	char *strval;
	int subtok;
}

%define parse.error verbose

%token <strval> EQUAL_SIGN
%token <strval> STRUCT_BEGIN
%token <strval> STRING_NAME
%token <strval> LEFT_CURLY_BRACKETS
%token <strval> RIGHT_CURLY_BRACKETS
%token <strval> SEMICOLON

%type <strval> structure complete_variable_body variable_body

%start complete_structure

%%

complete_structure : 
YYEOF
{
    return -1;
}
| structure SEMICOLON 
{ 
    printf("%s%s\n", $1, $2); 
    free($1);
    free($2);
    return 0;
};

structure : 
STRUCT_BEGIN STRING_NAME LEFT_CURLY_BRACKETS RIGHT_CURLY_BRACKETS
{
    $$ = malloc(1024);
    snprintf($$, 1024, "%s %s %s %s", $1, $2, $3, $4); 
    free($1);
    free($2);
    free($3);
    free($4);
}
| STRUCT_BEGIN STRING_NAME LEFT_CURLY_BRACKETS complete_variable_body RIGHT_CURLY_BRACKETS
{   
    $$ = malloc(1024);
    snprintf($$, 1024, "%s %s %s %s %s", $1, $2, $3, $4, $5); 
    free($1);
    free($2);
    free($3);
    free($4);
    free($5);
};


complete_variable_body : 
variable_body
{
    $$ = malloc(1024); 
    snprintf($$, 1024, "%s", $1);
    free($1);
}
| complete_variable_body variable_body 
{ 
    $$ = malloc(1024); 
    snprintf($$, 1024, "%s %s", $1, $2);
    free($1);
    free($2);
};

variable_body : STRING_NAME STRING_NAME SEMICOLON 
{ 
    $$ = malloc(1024);
    snprintf($$, 1024, "%s %s%s", $1, $2, $3);
    free($1);
    free($2);
    free($3);
}
| STRING_NAME STRING_NAME EQUAL_SIGN STRING_NAME SEMICOLON
{ 
    $$ = malloc(1024);
    snprintf($$, 1024, "%s %s%s%s%s", $1, $2, $3, $4, $5);
    free($1);
    free($2);
    free($3);
    free($4);
    free($5);
}
;

%%


main()
{
    extern FILE *yyin;
    if(!(yyin = fopen("test.h", "r"))) {
        return (1);
    }

    while (0 == yyparse());
}

yyerror(char *s)
{
    fprintf(stderr, "error: %s\n", s);
}